import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2023-06-11
 * Time: 12:59
 */
public class jdbcSelect {
    public static void main(String[] args) throws SQLException {
        // 1.创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java_mysql5?characterEncoding=utf8&&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");

        // 2.建立连接
        Connection connection = dataSource.getConnection();
        // 3.构造sql语句
        String sql = "select * from booklist";
        // 4.执行sql
        PreparedStatement statement = connection.prepareStatement(sql);
        // 5.执行sql，获取返回的表
        ResultSet resultSet = statement.executeQuery();
        // 6.使用迭代器遍历
        while (resultSet.next()) {//不为空
            String name = resultSet.getString(1);
            String author = resultSet.getString(2);
            int price = resultSet.getInt(3);
            String type = resultSet.getString(4);
            System.out.println("name = " + name + "  author = " + author + "  price = " + price + "  type = " + type);
        }
    }
}
